package Test06;

//Properties

//写出
import java.io.*;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Test40 {
    public static void main(String[] args) throws IOException {

        /*//Properties作为Map集合的基本操作
        //1、创建集合的对象
        Properties prop = new Properties();

        //2、添加数据
        prop.put("aaa", 111);
        prop.put("bbb", 222);
        prop.put("ccc", 333);
        prop.put("ddd", 444);

        //3、遍历集合

        //键找值
        Set<Object> keys = prop.keySet();
        for (Object key : keys) {
            Object value = prop.get(key);
            System.out.println(key + "=" + value);
        }

        System.out.println("=============");

        //键值对
        Set<Map.Entry<Object, Object>> entries = prop.entrySet();
        for (Map.Entry<Object, Object> entry : entries) {
            Object key = entry.getKey();
            Object value = entry.getValue();
            System.out.println(key + "=" + value);
        }

        System.out.println("====================================================");*/


        //Properties跟IO流结合的操作
        //1、创建集合
        Properties prop = new Properties();

        //2、添加数据
        prop.put("aaa", "bbb");
        prop.put("ccc", "ddd");
        prop.put("eee", "fff");
        prop.put("ggg", "hhh");

        //3、把集合中的数据以键值对的形式写到本地文件当中

        //特有方法
        FileOutputStream fos = new FileOutputStream("D:\\工作\\工作\\src\\a.properties");
        prop.store(fos, "test");
        fos.close();


        //常规写法
        /*BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\工作\\工作\\src\\a.properties"));
        Set<Map.Entry<Object, Object>> entries = prop.entrySet();
        for (Map.Entry<Object, Object> entry : entries) {
            Object key = entry.getKey();
            Object value = entry.getValue();
            bw.write(key + "=" + value);
            bw.newLine();
        }
        bw.close();*/
    }
}
